package com.adv.ems.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.adv.ems.db.entity.CollectPointDO;
import com.adv.ems.db.entity.EnergyDataOriginalDO;
import com.adv.ems.enums.CollectPointStateEnum;
import com.adv.ems.manager.EnergyDataOriginalManager;
import com.adv.ems.model.param.RemotePointAutoCreateParam;
import com.adv.ems.model.param.RemoteUploadDataParam;
import com.adv.ems.service.RemoteService;
import com.adv.ems.utils.RedisUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class RemoteServiceImpl extends BaseServiceImpl implements RemoteService {
    @Resource
    EnergyDataOriginalManager energyDataOriginalManager;
    @Resource
    RedisUtil redisUtil;

    @Override
    public Long pointAutoCreate(RemotePointAutoCreateParam param) {
        Optional<CollectPointDO> first = collectPointManager
                .getAllMap()
                .values()
                .stream()
                .filter(p -> param.getCode().equals(p.getUserCode()))
                .findFirst();
        if (first.isPresent()) {
            return first.get().getId();
        }
        CollectPointDO pointDO = new CollectPointDO();
        pointDO.setUserCode(param.getCode());
        pointDO.setName(StringUtils.hasLength(param.getName()) ? param.getName() : String.format("待处理采集点【%s】", DateUtil.format(new Date(), "yyyyMMddHH")));
        pointDO.setState(CollectPointStateEnum.CONFIRM.name());
        pointDO.setType("AUTO");
        pointDO.setCreateTime(new Date());
        pointDO.setUpdateTime(new Date());
        pointDO.setExt(JSONUtil.toJsonStr(MapUtil.of("orginal", JSONUtil.toJsonStr(param))));
        collectPointManager.saveOrUpdateData(pointDO);
        return pointDO.getId();
    }

    @Override
    public Integer uploadData(RemoteUploadDataParam param) {
        List<RemoteUploadDataParam.Item> list = param
                .getItems()
                .stream()
                .filter(i -> i.getPointId() != null && i.getColData() != null && StringUtils.hasLength(i.getColDate()))
                .filter(i -> collectPointManager.getAllMap().containsKey(i.getPointId()))
                .filter(item -> redisUtil.setNx(String.format("%s_%s", item.getPointId(), item.getColDate()), 30L))
                .collect(Collectors.toList());
        List<EnergyDataOriginalDO> checkedList = new ArrayList<>(list.size());
        for (RemoteUploadDataParam.Item item : list) {
            DateTime dateTime = DateUtil.parseDateTime(item.getColDate());
            EnergyDataOriginalDO data = new EnergyDataOriginalDO();
            data.setColData(item.getColData());
            data.setColDate(dateTime);
            data.setCollectPointId(item.getPointId());
            data.setCreateTime(new Date());
            data.setExt(item.getExt() != null ? JSONUtil.toJsonStr(item.getExt()) : null);
            checkedList.add(data);
        }
        int num = checkedList.size();
        if (!checkedList.isEmpty()) {
            energyDataOriginalManager.batchSaveData(checkedList);
        }
        return num;
    }
}
